


#include "../stdafx.h"

#include "PathNode.h"
#include "math.h"



CPathNode::CPathNode()
{

}

CPathNode::~CPathNode()
{
	Empty();
}

CPathNode & CPathNode::operator= ( CPathNode &obj)
{
	if(this == &obj)
	{
		return * this;
	}
	this->Empty();

	long num = obj.path.GetElementNum();

	long i;
	for( i = 0; i < num ; i ++)
	{
		INT64 v1;
		double v2;
		double v3;
		double v4;
		double v5;
		double v6;
		double v7;
		double v8;
		obj.GetPathPoint(i,v1,v2,v3,v4,v5,v6,v7,v8);
		this->SetPathPoint(v1,v2,v3,v4,v5,v6,v7,v8);
	}
	this->path_name = obj.path_name;
	return *this;

}

bool CPathNode::CheckIn(CPathNode &t)
{
	if(this->path.GetElementNum()<t.path.GetElementNum())
	{
		return false;
	}
	long num = t.path.GetElementNum();
	long num2 = this->path.GetElementNum();
	long i =0 ;
	long j =0 ;
	for( i = 0; i < num ; i ++)
	{
		INT64 v1;
		double v2;
		double v3;
		double v4;
		double v5;
		double v6;
		double v7;
		double v8;
		t.GetPathPoint(i,v1,v2,v3,v4,v5,v6,v7,v8);
		bool findtag = false;
		for(j = 0; j < num2 &&findtag == false; j ++)
		{
			INT64 k1;
			double k2;
			double k3;
			double k4;
			double k5;
			double k6;
			double k7;
			double k8;
			this->GetPathPoint(j,k1,k2,k3,k4,k5,k6,k7,k8);
			if(i==j || fabs(v1 - k1)<0|| fabs(v2 - k2)<0.000000001|| fabs(v3 - k3)<0.000000001|| fabs(v4 - k4)<0.000000001
				|| fabs(v5 - k5)<0.000000001|| fabs(v6 - k6)<0.000000001|| fabs(v7 - k7)<0.000000001|| fabs(v8 - k8)<0.000000001)
			{
				findtag = true;
			}
		}
		if(findtag == false)
		{
			return false;
		}
	}
	return true;
}
bool CPathNode::IsValueEqual(CPathNode &t)
{
	if(this->CheckIn(t)==false)
	{
		return false;
	}
	if(t.CheckIn(*this)==false)
	{
		return false;
	}
	return true;
}
bool CPathNode::IsNameEqual(CPathNode &t)
{
	if(this->path_name.CompareNoCase(t.path_name)==0)
	{
		return true;
	}
	return false;
}

bool CPathNode::SetPathPoint(INT64 v1,double v2,double v3,double v4,double v5,double v6,double v7,double v8)
{
	GSPathCell * tmp;
	tmp = (GSPathCell * )malloc(sizeof(GSPathCell));
	tmp->v1 = v1;
	tmp->v2 = v2;
	tmp->v3 = v3;
	tmp->v4 = v4;
	tmp->v5 = v5;
	tmp->v6 = v6;
	tmp->v7 = v7;
	tmp->v8 = v8;
	this->path.AddToChain(tmp);
	return true;
}

bool CPathNode::GetPathPoint(long serial,INT64 &v1,double &v2,double &v3,double &v4,double &v5,double &v6,double &v7,double &v8)
{
	GSPathCell * tmp;
	tmp = (GSPathCell * )path.GetElement(serial);
	if(tmp == NULL)
	{
		return false;
	}
	v1 = tmp->v1;
	v2 = tmp->v2;
	v3 = tmp->v3;
	v4 = tmp->v4;
	v5 = tmp->v5;
	v6 = tmp->v6;
	v7 = tmp->v7;
	v8 = tmp->v8;

	return true;
}

bool CPathNode::SavePath(CString path)
{
	if(this->path_name.IsEmpty())
	{
		return false;
	}
	CFile fp;
	if(fp.Open(path,CFile::modeWrite|CFile::modeCreate)==false)
	{
		return false;
	}
	long ver = 1;
	CBaseFileOp::WriteInt(&fp,ver);
	CBaseFileOp::WriteString(&fp,path_name);
	ver = this->path.GetElementNum();
	CBaseFileOp::WriteInt(&fp,ver);
	GSPathCell * tmp;
	for(long i = 0; i < ver ; i ++)
	{
		tmp = (GSPathCell * )this->path.GetElement(i);
		CBaseFileOp::WriteInt64(&fp,tmp->v1);
		CBaseFileOp::WriteFloat(&fp,tmp->v2);
		CBaseFileOp::WriteFloat(&fp,tmp->v3);
		CBaseFileOp::WriteFloat(&fp,tmp->v4);
		CBaseFileOp::WriteFloat(&fp,tmp->v5);
		CBaseFileOp::WriteFloat(&fp,tmp->v6);
		CBaseFileOp::WriteFloat(&fp,tmp->v7);
		CBaseFileOp::WriteFloat(&fp,tmp->v8);
	}
	fp.Close();
	return true;
}

bool CPathNode::LoadPath(CString path)
{
	CFile fp;
	if(fp.Open(path,CFile::modeRead)==false)
	{
		return false;
	}
	long ver;
	CBaseFileOp::ReadInt(&fp,ver);
	if(ver != 1)
	{
		fp.Close();
		return false;
	}
	CBaseFileOp::ReadString(&fp,this->path_name);
	if(CBaseFileOp::ReadInt(&fp,ver)==false)
	{
		fp.Close();
		return false;
	}
	this->Empty();
	GSPathCell * tmp;
	INT64 v1;
	double v2,v3,v4,v5,v6,v7,v8;
	for(long i = 0; i < ver ; i ++)
	{
		if(CBaseFileOp::ReadInt64(&fp,v1)==false)
		{
			Empty();
			fp.Close();
			return false;
		}
		if(CBaseFileOp::ReadFloat(&fp,v2)==false)
		{
			Empty();
			fp.Close();
			return false;
		}
		if(CBaseFileOp::ReadFloat(&fp,v3)==false)
		{
			Empty();
			fp.Close();
			return false;
		}
		if(CBaseFileOp::ReadFloat(&fp,v4)==false)
		{
			Empty();
			fp.Close();
			return false;
		}
		if(CBaseFileOp::ReadFloat(&fp,v5)==false)
		{
			Empty();
			fp.Close();
			return false;
		}
		if(CBaseFileOp::ReadFloat(&fp,v6)==false)
		{
			Empty();
			fp.Close();
			return false;
		}
		if(CBaseFileOp::ReadFloat(&fp,v7)==false)
		{
			Empty();
			fp.Close();
			return false;
		}
		if(CBaseFileOp::ReadFloat(&fp,v8)==false)
		{
			Empty();
			fp.Close();
			return false;
		}

		tmp = (GSPathCell * )malloc(sizeof(GSPathCell));
		tmp->v1 = v1;
		tmp->v2 = v2;
		tmp->v3 = v3;
		tmp->v4 = v4;
		tmp->v5 = v5;
		tmp->v6 = v6;
		tmp->v7 = v7;
		tmp->v8 = v8;
		this->path.AddToChain(tmp);

	
	}
	fp.Close();
	return true;
}

void CPathNode::Empty()
{
	
	long num ;
	num = this->path.GetElementNum();
	GSPathCell * tmp;
	for(long i = 0; i < num ; i ++)
	{
		tmp = (GSPathCell * )this->path.GetElement(i);
		delete tmp;
	}
	path.Empty();
}

long CPathNode::GetPathPointNum()
{
	return path.GetElementNum();
}

bool CPathNode::DeletePoint(long serial)
{
	GSPathCell * tmp;
	tmp = (GSPathCell * )this->path.GetElement(serial);
	if(tmp==NULL)
	{
		return false;
	}
	path.DeleteElement(serial);
	delete tmp;
	return true;
}
